<template>
  <div class="schedule-container">
    <el-row :gutter="20">
      <el-col :span="6">
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <div
              style="
                margin-right: 10px;
                text-align: center;
                margin-bottom: 20px;
              "
            >
              班级列表
            </div>
          </div>
          <el-tree
            :data="classes"
            node-key="id"
            :props="defaultProps"
            @node-click="handleNodeClick"
            highlight-current
          ></el-tree>
        </el-card>
      </el-col>
      <el-col :span="18">
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span
              >课表管理 -
              {{ selectedClass ? selectedClass.label : "请选择班级" }}</span
            >
            <el-button
              type="primary"
              @click="openAddDialog"
              :disabled="!selectedClass"
              style="margin-left: 30px"
              >添加课程</el-button
            >
          </div>
          <el-table
            :data="selectedClassSchedule"
            style="width: 100%; margin-top: 20px"
          >
            <el-table-column type="selection" min-width="30" />
            <el-table-column
              type="index"
              label="序号"
              min-width="60"
              align="center"
            >
              <template #default="scope">
                {{ scope.$index + 1 }}
              </template>
            </el-table-column>
            <el-table-column
              align="center"
              prop="courseName"
              label="课程名称"
            ></el-table-column>
            <el-table-column
              align="center"
              prop="classTime"
              label="上课时间"
            ></el-table-column>
            <el-table-column
              align="center"
              prop="number"
              label="节数"
            ></el-table-column>
            <el-table-column label="操作" align="center" min-width="200">
              <template #default="scope">
                <el-button
                  text
                  type="primary"
                  size="default"
                  @click="openEditDialog(scope.row)"
                  >编辑</el-button
                >
                <el-button
                  text
                  type="danger"
                  size="default"
                  @click="handleDelete(scope.row)"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
          <div class="pagination-container">
            <el-pagination
              v-model:current-page="currentPage"
              @current-change="handleCurrentChange"
              :page-size="pageSize"
              :pager-count="9"
              layout="prev, pager, next"
              :total="total"
              prev-text="上一页"
              next-text="下一页"
            />
          </div>
        </el-card>
      </el-col>
    </el-row>

    <el-dialog
      :title="dialogTitle"
      align-center
      v-model="dialogVisible"
      width="400px"
    >
      <el-form
        :model="newCourse"
        :rules="rules"
        ref="scoreForm"
        label-width="80px"
        class="dialog-form"
      >
        <el-form-item label="课程名" prop="courseId">
          <el-select
            v-model="newCourse.courseId"
            placeholder="请选择课程"
            class="dialog-input"
          >
            <el-option
              v-for="course in courses"
              :key="course.id"
              :label="course.name"
              :value="course.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="上课时间" prop="classTime">
          <el-date-picker
            v-model="newCourse.classTime"
            type="datetime"
            placeholder="请选择上课时间"
            class="dialog-input"
          />
        </el-form-item>
        <el-form-item label="节数" prop="number">
          <el-input
            v-model="newCourse.number"
            placeholder="请输入节数"
            class="dialog-input"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleSave">保存</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from "vue";
import {
  getAllClass,
  getClassSchedule,
  getAllCourse,
  addClassSchedule,
  updateClassSchedule,
  deleteClassSchedule,
} from "@/request/request";
import { ElMessage, ElMessageBox } from "element-plus";
const classes = ref([]);

const defaultProps = {
  children: "children",
  label: "label",
};
const selectedClass = ref(null);
const newCourse = ref({
  classId: "",
  courseId: "",
  classTime: "",
  number: "",
});
const currentPage = ref(1);
const pageSize = ref(10);
const total = ref(0);
const schedules = ref([]);
const courses = ref([]);
const dialogVisible = ref(false);
const search = ref("");
const dialogTitle = ref("");
const rules = ref({
  courseId: [{ required: true, message: "请选择课程", trigger: "blur" }],
  classTime: [{ required: true, message: "请选择上课时间", trigger: "blur" }],
  number: [
    { required: true, message: "请输入节数", trigger: "blur" },
    { pattern: /^[0-9]*$/, message: "请输入数字", trigger: "blur" },
  ],
});

onMounted(() => {
  getClassList();
  getCourseList();
});

function getClassList() {
  getAllClass().then((res) => {
    classes.value = res.data.map((item) => ({
      id: item.id,
      label: item.name,
    }));
  });
}

function getCourseList() {
  getAllCourse().then((res) => {
    courses.value = res.data;
  });
}

function getScheduleList() {
  getClassSchedule(
    selectedClass.value.id,
    currentPage.value,
    pageSize.value
  ).then((res) => {
    schedules.value = res.data.records;
    total.value = res.data.total;
  });
}
function handleCurrentChange(page) {
  currentPage.value = page;
  getScheduleList();
}
function handleNodeClick(data) {
  selectedClass.value = data;
  getScheduleList();
}
const selectedClassSchedule = computed(() => {
  return selectedClass.value ? schedules.value : [];
});

function openAddDialog() {
  newCourse.value = {
    classId: "",
    courseId: "",
    classTime: "",
    number: "",
  };
  newCourse.value.classId = selectedClass.value.id;
  dialogVisible.value = true;
  dialogTitle.value = "添加课程";
}

function handleSave() {
  if (newCourse.value.id) {
    updateClassSchedule(newCourse.value.id, newCourse.value).then((res) => {
      if (res.code == 200) {
        ElMessage.success("编辑成功");
        dialogVisible.value = false;
        getScheduleList();
      } else {
        ElMessage.error(res.msg);
      }
    });
  } else {
    addClassSchedule(newCourse.value).then((res) => {
      if (res.code == 200) {
        ElMessage.success("添加成功");
        dialogVisible.value = false;
        getScheduleList();
      } else {
        ElMessage.error(res.msg);
      }
    });
  }
}
function openEditDialog(row) {
  dialogVisible.value = true;
  dialogTitle.value = "编辑课表";
  newCourse.value = { ...row };
}
function handleDelete(row) {
  ElMessageBox.confirm("确定要删除该课表吗？", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      deleteClassSchedule(row.id).then((res) => {
        if (res.code == 200) {
          ElMessage.success("删除成功");
          getScheduleList();
        } else {
          ElMessage.error(res.msg);
        }
      });
    })
    .catch(() => {
      ElMessage.info("已取消删除");
    });
}
</script>

<style scoped>
html,
body {
  overflow: hidden;
  height: 100%;
  margin: 0;
}
.schedule-container {
  padding: 10px;
  box-sizing: border-box;
  overflow: hidden;
}
.box-card {
  margin-bottom: 20px;
  overflow: auto;
}
.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: center;
}
</style>
